package com.globalwave.toolbox.dictcode;

/**
 * @author virgil chen
 * 
 * @date Dec.8,2006
 */
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.globalwave.toolbox.DictCode;
import com.globalwave.toolbox.DictCodeSO;
import com.vanda.exception.VandaException;
import com.vandagroup.common.bo.UserContext;
import com.vandagroup.twpos.util.InitConfig;
import com.vandagroup.twpos.base.BOProxy;
import com.vandagroup.twpos.base.BaseAction;

public class DictCodeAction extends BaseAction {
	
    private String module = "paramList" ;
    
	public ActionForward doAdd(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
    throws Exception {		

		final DictCodeBOInf bo = getDictCodeBO();		
		
		bo.add(((DictCodeForm) form).getDictCodeDTO());
		
		InitConfig.reLoad("pl_parlst");
		
		return (mapping.findForward("default"));


	}

	public ActionForward toView(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {
	    
	    final DictCodeForm bean = (DictCodeForm) form;
	    final DictCodeBOInf bo = getDictCodeBO();		

	    final DictCode dto = bo.get(bean.getDictCodeDTO().getId());
		bean.setDictCodeDTO(dto);

		return (mapping.findForward("edit"));

	}

	public ActionForward doEdit(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {
	    
	    final DictCodeForm bean = (DictCodeForm) form;
	    final DictCodeBOInf bo = getDictCodeBO();

		bo.update(bean.getDictCodeDTO());
		
		InitConfig.reLoad("pl_parlst");
		
		return (mapping.findForward("default"));

	}

	public ActionForward doDelete(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {
	    
	    final DictCodeForm bean = (DictCodeForm) form;
	    final DictCodeBOInf bo = getDictCodeBO();
			
		bo.delete(bean.getIds());
		
		InitConfig.reLoad("pl_parlst");
		
		return (mapping.findForward("default"));
	}

	/*
	public ActionForward toQuery(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {
	    
		//request.getSession().removeAttribute("where");
		return (mapping.findForward("query"));

	}
	*/

	public ActionForward doQuery(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response,
			UserContext user) 
	throws Exception {

	    final DictCodeForm bean = (DictCodeForm) form;
	    final DictCodeBOInf bo = getDictCodeBO();

		final String strPageNO = (String) request.getParameter("pageNO");
		int pageNO = strPageNO == null ? 1 : Integer.parseInt(strPageNO);
			
		bean.getDictCodeSO().setPageNo(pageNO);
		request.setAttribute( "result", bo.query(bean.getDictCodeSO()) );

		return (mapping.findForward("list"));


	}

	/**
	 * ��������״̬�༭ҳ
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public ActionForward toEditOnlineSwitch(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {

	    final DictCodeForm bean = (DictCodeForm) form;
	    
	    final DictCodeSO so = bean.getDictCodeSO() ;
	    so.setParName("Control") ;	    
	    so.setParKey("USE_400_DATA") ;
	    
	    final DictCodeBOInf bo = getDictCodeBO();

	    final List result = bo.query(so).getReferenceObject() ;
	    
	    if (result.size() > 0) {
	        bean.setDictCodeDTO((DictCode) result.get(0)) ;
	    }
	    		
		return (mapping.findForward("toEditOnlineSwitch"));

	}

	/**
	 * ����״̬����
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception ����״̬�༭ҳ
	 */
	public ActionForward doEditOnlineSwitch(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {
	    
	    this.doEdit(mapping, form, request, response, user) ;
	     
	    return (mapping.findForward("reToEditOnlineSwitch"));
	}


	/**
	 * ����Control����༭ҳ
	 * <p/>
	 * 
	 * ��ͨ������Portal4Control���ӳ��ò��������� 
	 * parValue��ʽΪ��parameterLabel{optionValue:optionLabel;optionValue:optionLabel;......}
	 * ����л���������({})�����ʾ������ѡ����ֵ����ѡ��Ϊ�����������ݣ������ı��������ֵ��
	 * <p/>
	 * eg.
	 * ��������Ӳ���taxRate�����ã�
	 * ��������ݿ��pl_parlst�?����һ����¼��
	 * sql���£�
	 * ������ѡ�
	 * insert into pl_parlst(id, parname, parkey, parvalue) values(410, 'Portal4Control', 'taxRate', '˰��{0.05:5%;0.06:6%}') ;
	 * ʹ���ı����룬
	 * insert into pl_parlst(id, parname, parkey, parvalue) values(410, 'Portal4Control', 'taxRate', '˰��') ;
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public ActionForward toEditControl(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {

	    final DictCodeForm bean = (DictCodeForm) form;
	    
	    final DictCodeSO so = bean.getDictCodeSO() ;
	    final DictCodeBOInf bo = getDictCodeBO();

	    so.setParName("Control") ;
	    bean.setControls(bo.query(so).getReferenceObject()) ;

	    /*
	     * Portal4Control,
	     * format:parameterLabel{optionValue:optionLabel;optionValue:optionLabel;......}
	     * 
	     * */
	    so.setParName("Portal4Control") ;
	    final List portal4Controls = bo.query(so).getReferenceObject() ;
	    
	    int index;
	    DictCode param = null ;
	    for (int i = portal4Controls.size() - 1 ; i >= 0  ; i --) {
	        param = (DictCode)portal4Controls.get(i) ;
	        index = param.getParValue().indexOf("{") ;
	        if (index > 0) {
	            param.setRsvst1(param.getParValue().substring(index + 1, param.getParValue().length() - 1)) ;// options String
	            param.setParValue(param.getParValue().substring(0, index)) ;// label for display
	        }
	    }
	    
	    bean.setPortal4Controls(portal4Controls) ;
	    		
		return (mapping.findForward("toEditControl"));

	}

	/**
	 * Control�����
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception Control����༭ҳ
	 */
	public ActionForward doEditControl(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {

		final DictCodeBOInf bo = getDictCodeBO();	
		final DictCode dto = ((DictCodeForm) form).getDictCodeDTO() ;
		
	    this.doEdit(mapping, form, request, response, user) ;

        String path = mapping.findForward("reToEditControl").getPath() ;
        path += "&paramListDTO.parKey=" + dto.getParKey() ;
	     
        return new ActionForward(path, true) ;
	}

	/**
	 * �����ѯ13P����
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception ����״̬�༭ҳ
	 */
	public ActionForward doQueryPromotion13P(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {

	    final DictCodeForm bean = (DictCodeForm) form;
	    final DictCodeSO so = bean.getDictCodeSO() ;
	    if (so.getParName()== null || "".equals(so.getParName())) {
	        so.setParName("32342idfadfaewjkwefjj") ;// Ϊ�˲��һ���ս��
	        so.setParKey("PreferentialCodes") ;
	    }
        this.doQuery(mapping, form, request, response, user) ;
	     
	    return (mapping.findForward("listPromotion13P"));
	}
	

	/**
	 * ת��13P������ӽ���
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception ����״̬�༭ҳ
	 */
	public ActionForward toAddPromotion13P(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {
	    
	    //this.doEdit(mapping, form, request, response, user) ;
	     
	    return (mapping.findForward("editPromotion13P"));
	}


	/**
	 * ���13P����
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception ����״̬�༭ҳ
	 */
	public ActionForward doAddPromotion13P(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
    throws Exception {		

		final DictCodeBOInf bo = getDictCodeBO();	
		final DictCode dto = ((DictCodeForm) form).getDictCodeDTO() ;
		
		bo.addPromotion13P(dto);
		
		InitConfig.reLoad("pl_parlst");

        String path = mapping.findForward("reToPromotion13P").getPath() ;
        path += "&paramListSO.parName=" + dto.getParName() ;
	     
        ActionForward af = new ActionForward(path, true) ;
	    return af ;

	}
	


	/**
	 * ɾ��13P����
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 * @throws Exception ����״̬�༭ҳ
	 */
	public ActionForward doDeletePromotion13P(
	        ActionMapping mapping, 
	        ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response,
			UserContext user) 
	throws Exception {
	    
	    final DictCodeForm bean = (DictCodeForm) form;
	    final DictCodeBOInf bo = getDictCodeBO();
	    final DictCodeSO so = bean.getDictCodeSO() ;
			
		bo.delete(bean.getIds());
		
		InitConfig.reLoad("pl_parlst");

        String path = mapping.findForward("reToPromotion13P").getPath() ;
        path += "&paramListSO.parName=" + so.getParName() ;
	     
        ActionForward af = new ActionForward(path, true) ;
	    return af ;
	}
	
    public String getModule() {
        return this.module;
    }

    private DictCodeBOInf getDictCodeBO() throws VandaException {
        return (DictCodeBOInf) new BOProxy().newInstance(DictCodeBO.class);
    }
}
